using Microsoft.Win32;
using Microsoft.WindowsCE.Forms;
using System;

namespace EveSkillPocket
{
    internal class TodayPluginRegistry
    {
        private const string REGKEY_TODAY = @"Software\Microsoft\Today\Items\";
        private const string REGKEY_TODAY_WITH_ROOT = @"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\";

        public static void Install(string name, string dll, bool enabled, long flags, bool options, long selectability)
        {
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "DLL", dll, RegistryValueKind.String);
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "Enabled", enabled ? 1 : 0, RegistryValueKind.DWord);
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "Flags", flags, RegistryValueKind.DWord);
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "Options", options ? 1 : 0, RegistryValueKind.DWord);
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "Type", 4, RegistryValueKind.DWord);
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Today\Items\" + name, "Selectability", selectability, RegistryValueKind.DWord);
            SendMessage();
        }

        public static bool IsInstalled(string name)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Today\Items\" + name);
            if ((key != null) && (key.GetValue("DLL") != null))
            {
                key.Close();
                return true;
            }
            return false;
        }

        private static void SendMessage()
        {
            Message my_msg = new Message();
            my_msg = Message.Create((IntPtr)0xffff, 0x1a, (IntPtr)0xf2, IntPtr.Zero);
            MessageWindow.SendMessage(ref my_msg);
        }

        public static void Uninstall(string name)
        {
            Registry.LocalMachine.DeleteSubKey(@"Software\Microsoft\Today\Items\" + name);
            SendMessage();
        }
    }
}
